// -----------------------------------------------------------------------
// <copyright file="DonThuocChiTietData.cs" company="QSoft Viet Nam">
// TODO: Copyright © 2005-2012 QSoft Vietnam Corporation.
// </copyright>
// -----------------------------------------------------------------------
namespace ManagerClinic.Core.DataAccess
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using ManagerClinic.Core.Common;
    using System.Data.Common;
    using System.Data;
    using Microsoft.Practices.EnterpriseLibrary.Data;
    using Microsoft.Practices.EnterpriseLibrary.Common;
    using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;

    /// <summary>
    /// The class DonThuocChiTietData
    /// </summary>
    public class DonThuocChiTietDataAccess
    {
        /// <summary>
        /// The method support insert new DonThuocChiTietEntity into database
        /// </summary>
        /// <param name="entity">DonThuocChiTietEntity</param>
        /// <returns>Identify of new DonThuocChiTietEntity</returns>
        public bool InsertDonThuocChiTiet(DonThuocChiTietEntity obj)
        {
            try
            {
                string sql = "spDonThuocChiTiet_Insert";
                Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>();
                DbCommand cmd = db.GetStoredProcCommand(sql);
                db.AddInParameter(cmd, "MaVatTu", DbType.String, obj.MaVatTu);
                db.AddInParameter(cmd, "MaDonThuoc", DbType.String, obj.MaDonThuoc);
                db.AddInParameter(cmd, "SoLuong", DbType.Double, obj.SoLuong);
                db.AddInParameter(cmd, "MaCachDung", DbType.String, obj.MaCachDung);
                db.AddInParameter(cmd, "LanDung", DbType.Double, obj.LanDung);
                db.AddInParameter(cmd, "SoLuongDung", DbType.Double, obj.SoLuongDung);
                db.AddInParameter(cmd, "MaDonVi", DbType.String, obj.MaDonVi);
                db.AddInParameter(cmd, "MaDonViDung", DbType.String, obj.MaDonViDung);
                db.AddInParameter(cmd, "MaGhiChuCachDung", DbType.String, obj.MaGhiChuCachDung);
                db.AddInParameter(cmd, "MaPhanLoaiVatTu", DbType.String, obj.MaPhanLoaiVatTu);
                db.AddInParameter(cmd, "MaLoaiVatTu", DbType.String, obj.MaLoaiVatTu);
                int result = db.ExecuteNonQuery(cmd);

                return (result > 0) ? true : false;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// The method support to update
        /// </summary>
        /// <param name="entity">DonThuocChiTietEntity</param>
        /// <returns>true or false</returns>
        public bool EditDonThuocChiTiet(DonThuocChiTietEntity obj)
        {
            try
            {
                string sql = "spDonThuocChiTiet_Update";
                Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>();
                DbCommand cmd = db.GetStoredProcCommand(sql);
                db.AddInParameter(cmd, "MaVatTu", DbType.String, obj.MaVatTu);
                db.AddInParameter(cmd, "MaDonThuoc", DbType.String, obj.MaDonThuoc);
                db.AddInParameter(cmd, "SoLuong", DbType.Double, obj.SoLuong);
                db.AddInParameter(cmd, "MaCachDung", DbType.String, obj.MaCachDung);
                db.AddInParameter(cmd, "LanDung", DbType.Double, obj.LanDung);
                db.AddInParameter(cmd, "SoLuongDung", DbType.Double, obj.SoLuongDung);
                db.AddInParameter(cmd, "MaDonVi", DbType.String, obj.MaDonVi);
                db.AddInParameter(cmd, "MaDonViDung", DbType.String, obj.MaDonViDung);
                db.AddInParameter(cmd, "MaGhiChuCachDung", DbType.String, obj.MaGhiChuCachDung);
                db.AddInParameter(cmd, "MaPhanLoaiVatTu", DbType.String, obj.MaPhanLoaiVatTu);
                db.AddInParameter(cmd, "MaLoaiVatTu", DbType.String, obj.MaLoaiVatTu);
                int result = (int)db.ExecuteNonQuery(cmd);

                return (result > 0) ? true : false;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// The method remove a DonThuocChiTiet
        /// </summary>
        /// <param name="MaVatTu">MaVatTu</param>
        /// <param name="MaDonThuoc">MaDonThuoc</param>
        /// <returns>true or false</returns>
        public bool DeleteDonThuocChiTiet(string maVatTu, string maDonThuoc)
        {
            try
            {
                string sql = "spDonThuocChiTiet_DeleteByID";
                Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>();
                DbCommand cmd = db.GetStoredProcCommand(sql);
                db.AddInParameter(cmd, "MaVatTu", DbType.String, maVatTu);
                db.AddInParameter(cmd, "MaDonThuoc", DbType.String, maDonThuoc);

                int result = (int)db.ExecuteNonQuery(cmd);
                return (result > 0) ? true : false;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// The method support to get a DonThuocChiTietEntity
        /// </summary>
        /// <returns>DonThuocChiTietEntity</returns>
        public DonThuocChiTietEntity GetDonThuocChiTiet(string maVatTu, string maDonThuoc)
        {
            try
            {
                DonThuocChiTietEntity obj = null;
                string sql = "spDonThuocChiTiet_SelectByID";
                Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>();
                DbCommand cmd = db.GetStoredProcCommand(sql);
                db.AddInParameter(cmd, "MaVatTu", DbType.String, maVatTu);
                db.AddInParameter(cmd, "MaDonThuoc", DbType.String, maDonThuoc);
                using (IDataReader reader = db.ExecuteReader(cmd))
                {
                    while (reader.Read())
                    {
                        obj = new DonThuocChiTietEntity();
                        obj.MaVatTu = reader["MaVatTu"].ToString();
                        obj.MaDonThuoc = reader["MaDonThuoc"].ToString();
                        obj.SoLuong = String.IsNullOrEmpty(reader["SoLuong"].ToString()) == true ? 0 : (double)reader["SoLuong"];
                        obj.MaCachDung = reader["MaCachDung"].ToString();
                        obj.LanDung = String.IsNullOrEmpty(reader["LanDung"].ToString()) == true ? 0 : (double)reader["LanDung"];
                        obj.SoLuongDung = String.IsNullOrEmpty(reader["SoLuongDung"].ToString()) == true ? 0 : (double)reader["SoLuongDung"];
                        obj.MaDonVi = reader["MaDonVi"].ToString();
                        obj.MaDonViDung = reader["MaDonViDung"].ToString();
                        obj.MaGhiChuCachDung = reader["MaGhiChuCachDung"].ToString();
                        obj.MaLoaiVatTu = reader["MaLoaiVatTu"].ToString();
                        obj.MaPhanLoaiVatTu = reader["MaPhanLoaiVatTu"].ToString();
                    }
                }

                return obj;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// The method support to get all DonThuocChiTietEntity
        /// </summary>
        /// <returns>list of DonThuocChiTietEntity</returns>
        public List<DonThuocChiTietEntity> GetDonThuocChiTietList(string maDonThuoc)
        {
            try
            {
                List<DonThuocChiTietEntity> list = new List<DonThuocChiTietEntity>();
                DonThuocChiTietEntity obj = null;
                string sql = "spDonThuocChiTiet_SelectAll";
                Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>();
                DbCommand cmd = db.GetStoredProcCommand(sql);
                db.AddInParameter(cmd, "MaDonThuoc", DbType.String, maDonThuoc);
                using (IDataReader reader = db.ExecuteReader(cmd))
                {
                    while (reader.Read())
                    {
                        obj = new DonThuocChiTietEntity();
                        obj.MaVatTu = reader["MaVatTu"].ToString();
                        obj.TenVatTu = reader["TenVatTu"].ToString();
                        obj.MaDonThuoc = reader["MaDonThuoc"].ToString();
                        obj.SoLuong = String.IsNullOrEmpty(reader["SoLuong"].ToString()) == true ? 0 : (double)reader["SoLuong"];
                        obj.MaCachDung = reader["MaCachDung"].ToString();
                        obj.TenCachDung = reader["TenCachDung"].ToString();
                        obj.LanDung = String.IsNullOrEmpty(reader["LanDung"].ToString()) == true ? 0 : (double)reader["LanDung"];
                        obj.SoLuongDung = String.IsNullOrEmpty(reader["SoLuongDung"].ToString()) == true ? 0 : (double)reader["SoLuongDung"];
                        obj.MaDonVi = reader["MaDonVi"].ToString();
                        obj.TenDonVi = reader["TenDonVi"].ToString();
                        obj.MaDonViDung = reader["MaDonViDung"].ToString();
                        obj.TenDonViDung = reader["TenDonViDung"].ToString();
                        obj.MaGhiChuCachDung = reader["MaGhiChuCachDung"].ToString();
                        obj.TenGhiChuCachDung = reader["TenGhiChuCachDung"].ToString();
                        obj.MaLoaiVatTu = reader["MaLoaiVatTu"].ToString();
                        obj.MaPhanLoaiVatTu = reader["MaPhanLoaiVatTu"].ToString();
                        list.Add(obj);
                    }
                }

                return list;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

    }
}
